package com.cqc.manage.error;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.cqc.manage.dto.JsonResultDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@ControllerAdvice(basePackages = {"com.cqc.manage.controller"})
public class ErrorControllerAvice {

    private static Logger LOG = LoggerFactory.getLogger(ErrorControllerAvice.class);
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JSON handleException(Exception e){
        LOG.error("异常信息:{}",e.getMessage());
        String errorCode = e.getMessage();
        ErrorEnum byCode = ErrorEnum.getByCode(errorCode);
        return JSONUtil.parse(new JsonResultDto(byCode.getCode(), byCode.getMessage()));
    }
}
